import { _decorator, Animation, Component, Input, input, Node, tween, Vec3 } from 'cc';
import { aa } from '../frame/FrameCore';
import sceneMgr from '../manager/SceneMgr';
import commonMgr from '../manager/CommonMgr';
const { ccclass, property } = _decorator;
const tempV3 = new Vec3(0,0,0);

@ccclass('ComicCtrl')
export class ComicCtrl extends Component {
    @property(Animation) anm:Animation;
    @property(Node) startNode:Node;

    private _index =1
    private _cd = false;
    private _hand:Node = null;
    
    async start() {

        this.node.children.forEach((c)=>{
            c.scale = Vec3.ZERO;
        })
        input.on(Input.EventType.TOUCH_END,this.onTouchEnd,this);

        
    }

    close(){
        input.off(Input.EventType.TOUCH_END,this.onTouchEnd,this);
        sceneMgr.loadSelect();
        this.node.destroy();
    }

    onTouchEnd(){
        if(this._cd) return;
        if(this._index>4) return;
        if(this._index==1){
            this.startNode.destroy();
            aa.sound.play("s1",1)
            this.scheduleOnce(()=>{
                this._cd = false;
                this._hand = commonMgr.showHand(Vec3.ZERO);
            },2.2)


        } else if(this._index==2){
            tempV3.y =-200;
            tween(this.node).to(0.5,{position:tempV3}).start();
            aa.sound.play("s2",1)
            if(this._hand){
                aa.res.put(this._hand)
            }
            this.scheduleOnce(()=>{
                this._cd = false;
                this._hand = commonMgr.showHand(Vec3.ZERO);
            },2.5)

        }else if (this._index==3){
            tempV3.y =0;
            tween(this.node).to(0.5,{position:tempV3}).start();
            aa.sound.play("s3",1)
            if(this._hand){
                aa.res.put(this._hand)
            }
            this.scheduleOnce(()=>{
                this._cd = false;
                this._hand = commonMgr.showHand(Vec3.ZERO);

            },3.1)
        }
        else{
            if(this._hand){
                aa.res.put(this._hand)
            }
            aa.sound.play("s4",1)
            this.scheduleOnce(()=>{
                this.close();
            },4.35)

        }

        this.anm.play("anm"+this._index);

        this._index++;

        this._cd = true;

      



    }

    update(deltaTime: number) {
        
    }
}

